/******************************************************************************
 * * setting_profile.c - implementation of profile setting functions
 *
 * *(C) Copyright 2019 Asr International Ltd.
 * * All Rights Reserved
 * ******************************************************************************/

#include "setting_global.h"

Setting_Profile_Setting_Mng_t *g_SettingProfileMng                = NULL;
VOID                          *gp_SettingProfileSettingCurUIDesc  = NULL;              // Current UI description
TIMER_ID                      g_SettingProfileSettingProfileTimer = NULL;              // for profile timed function, one time timer
UINT8                         g_SettingProfileActivateIdx;                             // to save current activate profile index
UINT8                         g_SettingProfileOperatingIdx;                            // to save current operating profile index

/**
 * sub-module entry
 * param (in) void
 * return  void
 */
VOID Setting_Profile_Main(VOID)
{
    // init manage variable
}

/**
 * sub-module event handler
 * param (in) void
 * return  void
 */
VOID Setting_Profile_Event_handle(VOID)
{
    // event and message dispatch
}

/**
 * initiate profile sub-module
 * param (in) pNvPara: NV_UI_Settings_t
 * return  void
 */
VOID Setting_Profile_Init_Req(NV_UI_Settings_t *pNvPara)
{
    g_SettingProfileActivateIdx = pNvPara->Profile_Setting.ProfileActivateIdx;
    g_SettingProfileOperatingIdx = pNvPara->Profile_Setting.ProfileActivateIdx;

    // inform desktop silent or not
    if(SETTING_PROFILE_SILENT == g_SettingProfileActivateIdx) {
        Setting_Ring_Silent_Ind(TRUE);
    } else {
        Setting_Ring_Silent_Ind(FALSE);
    }
}

VOID Setting_Profile_Setting_Update(Setting_Tone_Setting_Mng_t *SettingTone)
{
    if(NULL == SettingTone) {
        printf("%s: input parameter(SettingTone) is NULL!\n", __FUNCTION__);
    } else {
        printf("%s: address of inprt parameter(SettingTone) is %p\n", __FUNCTION__, SettingTone);
        if(NULL == g_SettingProfileMng) {
            printf("%s: g_SettingProfileMng is NULL!\n", __FUNCTION__);
        } else {
            printf("%s: update ProfileSetting[%d]\n", __FUNCTION__, g_SettingProfileMng->CurrentEditingIdx);
            Hal_Mem_Set(&g_SettingProfileMng->ProfileSetting[g_SettingProfileMng->CurrentEditingIdx], 0, sizeof(Setting_Tone_Setting_Mng_t));
            Hal_Mem_Copy(&g_SettingProfileMng->ProfileSetting[g_SettingProfileMng->CurrentEditingIdx], SettingTone, sizeof(Setting_Tone_Setting_Mng_t));
        }
    }
}

/**
 * Write ring tone to NV
 * param (in) Index: UINT8
 * param (in) SimId: MMI_MODEM_SIM_ID
 * param (in) nvData: UINT8
 * return void:
 */
VOID Setting_Profile_Write_Ring_Tone_To_Nv(UINT8 Index, MMI_MODEM_SIM_ID SimId, UINT8 nvData)
{
    UINT8 offset = NV_SETTING_PROFILE_SETTING_OFFSET + 1 + NV_SETTING_TONE_SETTING_LEN * Index;

    printf("%s: CurrentRingToneIndex[%d] = %d\n", __FUNCTION__, SimId, nvData);
    // nv write
    if(MMI_MODEM_SIM_1 == SimId) {
        UI_NV_Write_Req(NV_SECTION_UI_SETTINGS, offset, 1, &nvData);
    } else {
        UI_NV_Write_Req(NV_SECTION_UI_SETTINGS, (offset + 1), 1, &nvData);
    }
}

/**
 * Write ring Volume to NV
 * param (in) Index: UINT8
 * param (in) nvData: UINT8
 * return void:
 */
VOID Setting_Profile_Write_Ring_Volume_To_Nv(UINT8 Index, UINT8 nvData)
{
    UINT8 offset = NV_SETTING_PROFILE_SETTING_OFFSET + 1 + NV_SETTING_TONE_SETTING_LEN * Index;

    printf("%s: RingVolume = %d\n", __FUNCTION__, nvData);
    // nv write
    UI_NV_Write_Req(NV_SECTION_UI_SETTINGS, (offset + 2), 1, &nvData);
}

/**
 * Write keypad tone volume to NV
 * param (in) Index: UINT8
 * param (in) nvDat: UINT8
 * return void:
 */
VOID Setting_Profile_Write_Keypad_Tone_Volume_To_Nv(UINT8 Index, UINT8 nvData)
{
    UINT8 offset = NV_SETTING_PROFILE_SETTING_OFFSET + 1 + NV_SETTING_TONE_SETTING_LEN * Index;

    printf("%s: KeypadToneVolume = %d\n", __FUNCTION__, nvData);
    // nv write
    UI_NV_Write_Req(NV_SECTION_UI_SETTINGS, (offset + 3), 1, &nvData);
}

/**
 * Write vibrating state to NV
 * param (in) Index: UINT8
 * param (in) nvData: UINT8
 * return void:
 */
VOID Setting_Profile_Write_Vibrating_To_Nv(UINT8 Index, UINT8 nvData)
{
    UINT8 offset = NV_SETTING_PROFILE_SETTING_OFFSET + 1 + NV_SETTING_TONE_SETTING_LEN * Index;

    printf("%s: VibratingStat = %d\n", __FUNCTION__, nvData);
    // nv write
    UI_NV_Write_Req(NV_SECTION_UI_SETTINGS, (offset + 4), 1, &nvData);
}

/**
 * Write message alert state to NV
 * param (in) Index: UINT8
 * param (in) SimId: MMI_MODEM_SIM_ID
 * param (in) nvData: UINT8
 * return void:
 */
VOID Setting_Profile_Write_Alert_Stat_To_Nv(UINT8 Index, MMI_MODEM_SIM_ID SimId, UINT8 nvData)
{
    UINT8 offset = NV_SETTING_PROFILE_SETTING_OFFSET + 1 + NV_SETTING_TONE_SETTING_LEN * Index;

    printf("%s: MsgAlertStat[%d] = %d\n", __FUNCTION__, SimId, nvData);
    // nv write
    if(MMI_MODEM_SIM_1 == SimId) {
        UI_NV_Write_Req(NV_SECTION_UI_SETTINGS, (offset + 5), 1, &nvData);
    } else {
        UI_NV_Write_Req(NV_SECTION_UI_SETTINGS, (offset + 6), 1, &nvData);
    }
}

/**
 * Write message tone to NV
 * param (in) Index: UINT8
 * param (in) SimId: MMI_MODEM_SIM_ID
 * param (in) nvData: UINT8
 * return void:
 */
VOID Setting_Profile_Write_Message_Tone_To_Nv(UINT8 Index, MMI_MODEM_SIM_ID SimId, UINT8 nvData)
{
    UINT8 offset = NV_SETTING_PROFILE_SETTING_OFFSET + 1 + NV_SETTING_TONE_SETTING_LEN * Index;

    printf("%s: CurrentMsgToneIndex[%d] = %d\n", __FUNCTION__, SimId, nvData);
    // nv write
    if(MMI_MODEM_SIM_1 == SimId) {
        UI_NV_Write_Req(NV_SECTION_UI_SETTINGS, (offset + 7), 1, &nvData);
    } else {
        UI_NV_Write_Req(NV_SECTION_UI_SETTINGS, (offset + 8), 1, &nvData);
    }
}

/**
 * Write warning tone to NV
 * param (in) Index: UINT8
 * param (in) nvData: UINT8
 * return void:
 */
VOID Setting_Profile_Write_Warning_Tone_To_Nv(UINT8 Index, UINT8 nvData)
{
    UINT8 offset = NV_SETTING_PROFILE_SETTING_OFFSET + 1 + NV_SETTING_TONE_SETTING_LEN * Index;

    printf("%s: WarningToneStat = %d\n", __FUNCTION__, nvData);
    // nv write
    UI_NV_Write_Req(NV_SECTION_UI_SETTINGS, (offset + 9), 1, &nvData);
}

/**
 * Save activated profile setting to NV
 * param (in) void:
 * return  void:
 * return  void:
 */
VOID Display_Setting_Done_Profile_Save2NV(VOID)
{
    UINT8 nvData[NV_SETTING_TONE_SETTING_LEN + 1];

    // nv write
    nvData[0] = g_SettingToneSettingMng.CurrentRingToneIndex[0];
    nvData[1] = g_SettingToneSettingMng.CurrentRingToneIndex[1];
    nvData[2] = g_SettingToneSettingMng.RingVolume;
    nvData[3] = g_SettingToneSettingMng.KeypadToneVolume;
    nvData[4] = g_SettingToneSettingMng.VibratingStat;
    nvData[5] = g_SettingToneSettingMng.MsgAlertStat[0];
    nvData[6] = g_SettingToneSettingMng.MsgAlertStat[1];
    nvData[7] = g_SettingToneSettingMng.CurrentMsgToneIndex[0];
    nvData[8] = g_SettingToneSettingMng.CurrentMsgToneIndex[1];
    nvData[9] = g_SettingToneSettingMng.WarningToneStat;
    nvData[10] = g_SettingProfileActivateIdx;

    // set tone nv
    UI_NV_Write_Req(NV_SECTION_UI_SETTINGS, NV_SETTING_TONE_SETTING_OFFSET, NV_SETTING_TONE_SETTING_LEN, nvData);
    // set profile nv
    UI_NV_Write_Req(NV_SECTION_UI_SETTINGS, NV_SETTING_PROFILE_SETTING_OFFSET, 1, &nvData[10]);
}

VOID Display_Setting_Profile_X_Activate_Cb(lv_obj_t *Btn, lv_event_t Event)
{
    if (LV_EVENT_CLICKED == Event)
    {
        UI_Normal_List_Only_Desc_t *NormalLstOnlyDesc;
        UI_PHONE_TEXT_ID           TxtId;

        NormalLstOnlyDesc             = (UI_Normal_List_Only_Desc_t *)gp_SettingProfileSettingCurUIDesc;
        NormalLstOnlyDesc->List.Index = 0;

        switch (g_SettingProfileMng->CurrentEditingIdx)
        {
        case SETTING_PROFILE_GENERAL:
            TxtId = PHONE_TEXT_ID_GENERAL;
            Setting_Ring_Silent_Ind(FALSE);
            break;

        case SETTING_PROFILE_SILENT:
            TxtId = PHONE_TEXT_ID_SILENT;
            Setting_Ring_Silent_Ind(TRUE);
            break;

        case SETTING_PROFILE_DISCREET:
            TxtId = PHONE_TEXT_ID_DISCREET;
            Setting_Ring_Silent_Ind(FALSE);
            break;

        case SETTING_PROFILE_LOUD:
            TxtId = PHONE_TEXT_ID_LOUD;
            Setting_Ring_Silent_Ind(FALSE);
            break;

        case SETTING_PROFILE_MY_STYLE:
            TxtId = PHONE_TEXT_ID_MY_STYLE;
            Setting_Ring_Silent_Ind(FALSE);
            break;
        }

        // stop profile timer when set activate
        if(g_SettingProfileSettingProfileTimer) {
            Hal_Timer_Stop(g_SettingProfileSettingProfileTimer);
            g_SettingProfileSettingProfileTimer = NULL;
            Setting_Update_Profile_Para(2, 0);
        }

        // Copy profile to tone setting manage variable
        g_SettingProfileActivateIdx = g_SettingProfileMng->CurrentEditingIdx;
        g_SettingProfileOperatingIdx = g_SettingProfileMng->CurrentEditingIdx;
        Setting_Update_Profile_Para(1, g_SettingProfileActivateIdx);
        Setting_Update_Profile_Para(3, g_SettingProfileOperatingIdx);
        Hal_Mem_Copy(&g_SettingToneSettingMng, &g_SettingProfileMng->ProfileSetting[g_SettingProfileActivateIdx],sizeof(Setting_Tone_Setting_Mng_t));
        Display_Setting_Done_Profile_Save2NV();
        Display_Setting_Done(ACT_ID_SETTING_PROFILE_SETTING, TxtId, ACT_ID_DESKTOP);
    }
}

VOID Display_Setting_Profile_X_Personalise_Cb(lv_obj_t *Btn, lv_event_t Event)
{
    if (LV_EVENT_CLICKED == Event)
    {
        UI_Normal_List_Only_Desc_t *NormalLstOnlyDesc;

        NormalLstOnlyDesc             = (UI_Normal_List_Only_Desc_t *)gp_SettingProfileSettingCurUIDesc;
        NormalLstOnlyDesc->List.Index = 1;

        Setting_Update_Profile_Para(0, g_SettingProfileMng->CurrentEditingIdx);
        Setting_Set_Operating_Tone(&g_SettingProfileMng->ProfileSetting[g_SettingProfileMng->CurrentEditingIdx]);
        Display_Setting_Tone_Settings();
    }
}

VOID Display_Setting_Profile_X_Timed_Cb(lv_obj_t *Btn, lv_event_t Event)
{
    if (LV_EVENT_CLICKED == Event)
    {
        UI_Normal_List_Only_Desc_t *NormalLstOnlyDesc;

        NormalLstOnlyDesc             = (UI_Normal_List_Only_Desc_t *)gp_SettingProfileSettingCurUIDesc;
        NormalLstOnlyDesc->List.Index = 2;

        Display_Setting_Time_Scroll(ACT_ID_SETTING_PROFILE_SETTING);
    }
}

/**
 *
 * Callback of Setting_Profile_X, on destroy
 * param (in) NormalLstOnlyDesc: UI_Normal_List_Only_Desc_t*
 * return void:
 */
VOID Display_Setting_Profile_X_Destroy(VOID *pPara)
{
    UI_Normal_List_Only_Desc_t *NormalLstOnlyDesc = NULL;

    NormalLstOnlyDesc = (UI_Normal_List_Only_Desc_t *)pPara;

    Hal_Mem_Free(NormalLstOnlyDesc->List.ButtonList);
    Hal_Mem_Free(NormalLstOnlyDesc);
}

/**
 *
 * Callback of Setting_Profile_X, KeyCb
 * param (in) obj: lv_obj_t*
 *       (in) key_sta: UI_KEY_STATUS
 *       (in) key_val: UI_KEY_VALUE
 * return UINT8: 0--call system handling outside
 *               1--handled inside
 */
UINT8 Display_Setting_Profile_X_KeyCb(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val)
{
    if (KEY_RELEASED != key_sta)
    {
        return(0);
    }

    if (KEY_SOFTRIGHT == key_val)
    {
        Nav_Back(ACT_ID_SETTING_PROFILE_SETTING);
        return(1);
    }
    else if (KEY_END == key_val)
    {
        Nav_Home(ACT_ID_ANY);
        return(1);
    }
    else
    {
        return(0);
    }
}

/**
 * display Setting_Profile_Setting (General)
 * param (in) void:
 * return void:
 */
VOID Display_Setting_Profile_X(VOID)
{
    // Display Setting_Profile_X
    UI_Normal_List_Only_Desc_t *NormalLstOnlyDesc;
    Nav_Func_List_t            FuncList;

    NormalLstOnlyDesc = (UI_Normal_List_Only_Desc_t *)Hal_Mem_Alloc(sizeof(UI_Normal_List_Only_Desc_t));
    Hal_Mem_Set(NormalLstOnlyDesc, 0, sizeof(UI_Normal_List_Only_Desc_t));

    switch (g_SettingProfileMng->CurrentEditingIdx)
    {
    case SETTING_PROFILE_GENERAL:
        NormalLstOnlyDesc->TitleBar.TxtId = PHONE_TEXT_ID_GENERAL;
        break;

    case SETTING_PROFILE_SILENT:
        NormalLstOnlyDesc->TitleBar.TxtId = PHONE_TEXT_ID_SILENT;
        break;

    case SETTING_PROFILE_DISCREET:
        NormalLstOnlyDesc->TitleBar.TxtId = PHONE_TEXT_ID_DISCREET;
        break;

    case SETTING_PROFILE_LOUD:
        NormalLstOnlyDesc->TitleBar.TxtId = PHONE_TEXT_ID_LOUD;
        break;

    case SETTING_PROFILE_MY_STYLE:
        NormalLstOnlyDesc->TitleBar.TxtId = PHONE_TEXT_ID_MY_STYLE;
        break;
    }
    NormalLstOnlyDesc->TitleBar.TxtAlign   = LV_LABEL_ALIGN_CENTER;
    NormalLstOnlyDesc->TitleBar.LabelAlign = LV_ALIGN_IN_TOP_LEFT;

    NormalLstOnlyDesc->List.Index         = 0;
    NormalLstOnlyDesc->List.ListButtonCnt = 3;
    NormalLstOnlyDesc->List.ButtonList    = (UI_Button_Desc_t *)Hal_Mem_Alloc(sizeof(UI_Button_Desc_t) * 3);
    Hal_Mem_Set(NormalLstOnlyDesc->List.ButtonList, 0, sizeof(UI_Button_Desc_t) * 3);

    NormalLstOnlyDesc->List.ButtonList[0].Valid       = TRUE;
    NormalLstOnlyDesc->List.ButtonList[0].ButtonIndex = 0;
    NormalLstOnlyDesc->List.ButtonList[0].TxtId       = PHONE_TEXT_ID_ACTIVATE;
    NormalLstOnlyDesc->List.ButtonList[0].ReleaseFun  = Display_Setting_Profile_X_Activate_Cb;
    NormalLstOnlyDesc->List.ButtonList[1].Valid       = TRUE;
    NormalLstOnlyDesc->List.ButtonList[1].ButtonIndex = 1;
    NormalLstOnlyDesc->List.ButtonList[1].TxtId       = PHONE_TEXT_ID_PERSONALISE;
    NormalLstOnlyDesc->List.ButtonList[1].ReleaseFun  = Display_Setting_Profile_X_Personalise_Cb;
    NormalLstOnlyDesc->List.ButtonList[2].Valid       = TRUE;
    NormalLstOnlyDesc->List.ButtonList[2].ButtonIndex = 2;
    NormalLstOnlyDesc->List.ButtonList[2].TxtId       = PHONE_TEXT_ID_TIMED;
    NormalLstOnlyDesc->List.ButtonList[2].ReleaseFun  = Display_Setting_Profile_X_Timed_Cb;

    NormalLstOnlyDesc->BtnBar.ButtonL.Valid = FALSE;
    NormalLstOnlyDesc->BtnBar.ButtonM.Valid = TRUE;
    NormalLstOnlyDesc->BtnBar.ButtonM.TxtId = PHONE_TEXT_ID_OK;
    NormalLstOnlyDesc->BtnBar.ButtonR.Valid = TRUE;
    NormalLstOnlyDesc->BtnBar.ButtonR.TxtId = PHONE_TEXT_ID_BACK;

    NormalLstOnlyDesc->KeyFunc = Display_Setting_Profile_X_KeyCb;

    Hal_Mem_Set(&FuncList, 0, sizeof(Nav_Func_List_t));
    FuncList.OnShow    = UI_Normal_List_Only_Create;
    FuncList.OnDestroy = Display_Setting_Profile_X_Destroy;

    gp_SettingProfileSettingCurUIDesc = (VOID *)NormalLstOnlyDesc;
    Nav_Forward(ACT_ID_SETTING_PROFILES_LIST, ACT_ID_SETTING_PROFILE_SETTING, &FuncList, (VOID *)NormalLstOnlyDesc);
} /* Display_Setting_Profile_X */

/**
 *
 * Callback of Setting_Profiles_List, on create
 * param (in) NormalLstOnlyDesc: UI_Normal_List_Only_Desc_t*
 * return void:
 */
VOID Display_Setting_Profiles_List_Create(VOID *pPara)
{
    UINT8  i;
    UINT16 Pos = 1;
    UINT8  nvData[NV_SETTING_PROFILE_SETTING_LEN];

    // malloc memory for manage variable
    g_SettingProfileMng = (Setting_Profile_Setting_Mng_t *)Hal_Mem_Alloc(sizeof(Setting_Profile_Setting_Mng_t));
    printf("%s: g_SettingProfileMng malloc proc, address = %p\n", __FUNCTION__, g_SettingProfileMng);

    UI_NV_Read_Req(NV_SECTION_UI_SETTINGS, NV_SETTING_PROFILE_SETTING_OFFSET, NV_SETTING_PROFILE_SETTING_LEN, nvData);

    // init manage variable
    for (i = 0; i < NV_SETTING_MAX_PROFILE_NUM; i++)
    {
        // Hal_Mem_Copy(&g_SettingProfileMng->ProfileName[i], &pNvPara->Profile_Setting.ProfileName[i], NV_SETTING_MAX_PROFILE_NAME_LEN);
        g_SettingProfileMng->ProfileSetting[i].CurrentRingToneIndex[0] = nvData[Pos++];
        g_SettingProfileMng->ProfileSetting[i].CurrentRingToneIndex[1] = nvData[Pos++];
        g_SettingProfileMng->ProfileSetting[i].RingVolume              = nvData[Pos++];
        g_SettingProfileMng->ProfileSetting[i].KeypadToneVolume        = nvData[Pos++];
        g_SettingProfileMng->ProfileSetting[i].VibratingStat           = (SETTING_VIBRATING_STAT)nvData[Pos++];
        g_SettingProfileMng->ProfileSetting[i].MsgAlertStat[0]         = (SETTING_MSG_ALERT_STAT)nvData[Pos++];
        g_SettingProfileMng->ProfileSetting[i].MsgAlertStat[1]         = (SETTING_MSG_ALERT_STAT)nvData[Pos++];
        g_SettingProfileMng->ProfileSetting[i].CurrentMsgToneIndex[0]  = nvData[Pos++];
        g_SettingProfileMng->ProfileSetting[i].CurrentMsgToneIndex[1]  = nvData[Pos++];
        g_SettingProfileMng->ProfileSetting[i].WarningToneStat         = (SETTING_WARNING_TONE_STAT)nvData[Pos++];
    }

    g_SettingProfileMng->CurrentEditingIdx = g_SettingProfileOperatingIdx;
}

/**
 *
 * Callback of Setting_Profile_Settings, on destroy
 * param (in) NormalLstOnlyDesc: UI_Normal_List_Only_Desc_t*
 * return void:
 */
VOID Display_Setting_Profiles_List_Destroy(VOID *pPara)
{
    UI_Normal_List_Only_Desc_t *NormalLstOnlyDesc = NULL;

    NormalLstOnlyDesc = (UI_Normal_List_Only_Desc_t *)pPara;

    Hal_Mem_Free(NormalLstOnlyDesc->List.ButtonList);
    Hal_Mem_Free(NormalLstOnlyDesc);

    if(NULL != g_SettingProfileMng) {
        printf("%s: g_SettingProfileMng free proc, address = %p\n", __FUNCTION__, g_SettingProfileMng);
        Hal_Mem_Free(g_SettingProfileMng);
        g_SettingProfileMng = NULL;
    }
}

/**
 * display Setting_Error for timed profile
 * param (in) void:
 * return void:
 */
VOID Display_Setting_Error_Profile(VOID)
{
    Display_Setting_Error(ACT_ID_SETTING_TIME_SCROLL, PHONE_TEXT_ID_TIMER_LEN_TIP, ACT_ID_SETTING_TIME_SCROLL, TRUE);
}

VOID Display_Setting_Profile_General_Cb(lv_obj_t *Btn, lv_event_t Event)
{
    if (LV_EVENT_CLICKED == Event)
    {
        UI_Normal_List_Only_Desc_t *NormalLstOnlyDesc;

        NormalLstOnlyDesc             = (UI_Normal_List_Only_Desc_t *)gp_SettingProfileSettingCurUIDesc;
        NormalLstOnlyDesc->List.Index = 0;

        g_SettingProfileMng->CurrentEditingIdx = SETTING_PROFILE_GENERAL;
        Display_Setting_Profile_X();
    }
}

VOID Display_Setting_Profile_Silent_Cb(lv_obj_t *Btn, lv_event_t Event)
{
    if (LV_EVENT_CLICKED == Event)
    {
        UI_Normal_List_Only_Desc_t *NormalLstOnlyDesc;

        NormalLstOnlyDesc             = (UI_Normal_List_Only_Desc_t *)gp_SettingProfileSettingCurUIDesc;
        NormalLstOnlyDesc->List.Index = 1;

        g_SettingProfileMng->CurrentEditingIdx = SETTING_PROFILE_SILENT;
        Display_Setting_Profile_X();
    }
}

VOID Display_Setting_Profile_Discreet_Cb(lv_obj_t *Btn, lv_event_t Event)
{
    if (LV_EVENT_CLICKED == Event)
    {
        UI_Normal_List_Only_Desc_t *NormalLstOnlyDesc;

        NormalLstOnlyDesc             = (UI_Normal_List_Only_Desc_t *)gp_SettingProfileSettingCurUIDesc;
        NormalLstOnlyDesc->List.Index = 2;

        g_SettingProfileMng->CurrentEditingIdx = SETTING_PROFILE_DISCREET;
        Display_Setting_Profile_X();
    }
}

VOID Display_Setting_Profile_Loud_Cb(lv_obj_t *Btn, lv_event_t Event)
{
    if (LV_EVENT_CLICKED == Event)
    {
        UI_Normal_List_Only_Desc_t *NormalLstOnlyDesc;

        NormalLstOnlyDesc             = (UI_Normal_List_Only_Desc_t *)gp_SettingProfileSettingCurUIDesc;
        NormalLstOnlyDesc->List.Index = 3;

        g_SettingProfileMng->CurrentEditingIdx = SETTING_PROFILE_LOUD;
        Display_Setting_Profile_X();
    }
}

VOID Display_Setting_Profile_MyStyle_Cb(lv_obj_t *Btn, lv_event_t Event)
{
    if (LV_EVENT_CLICKED == Event)
    {
        UI_Normal_List_Only_Desc_t *NormalLstOnlyDesc;

        NormalLstOnlyDesc             = (UI_Normal_List_Only_Desc_t *)gp_SettingProfileSettingCurUIDesc;
        NormalLstOnlyDesc->List.Index = 4;

        g_SettingProfileMng->CurrentEditingIdx = SETTING_PROFILE_MY_STYLE;
        Display_Setting_Profile_X();
    }
}

/**
 * Resume default profile from NV after timed profile expired
 * param (in) void:
 * return  void:
 */
VOID Display_Setting_Timed_Profile_Resume_From_NV(VOID)
{
    UINT8 nvData[NV_SETTING_TONE_SETTING_LEN];

    // nv read
    UI_NV_Read_Req(NV_SECTION_UI_SETTINGS, NV_SETTING_TONE_SETTING_OFFSET, NV_SETTING_TONE_SETTING_LEN, nvData);

    // resume the tone setting stored in NV
    g_SettingToneSettingMng.CurrentRingToneIndex[0] = nvData[0];
    g_SettingToneSettingMng.CurrentRingToneIndex[1] = nvData[1];
    g_SettingToneSettingMng.RingVolume              = nvData[2];
    g_SettingToneSettingMng.KeypadToneVolume        = nvData[3];
    g_SettingToneSettingMng.VibratingStat           = (SETTING_VIBRATING_STAT)nvData[4];
    g_SettingToneSettingMng.MsgAlertStat[0]         = (SETTING_MSG_ALERT_STAT)nvData[5];
    g_SettingToneSettingMng.MsgAlertStat[1]         = (SETTING_MSG_ALERT_STAT)nvData[6];
    g_SettingToneSettingMng.CurrentMsgToneIndex[0]  = nvData[7];
    g_SettingToneSettingMng.CurrentMsgToneIndex[1]  = nvData[8];
    g_SettingToneSettingMng.WarningToneStat         = (SETTING_WARNING_TONE_STAT)nvData[9];
}

/**
 *
 * Callback of timed profile timer
 * param (in) Para: UINT32
 * return void:
 */
VOID Display_Setting_Timed_Profile_Timeout(VOID* Para)
{
    if(SETTING_PROFILE_SILENT == g_SettingProfileActivateIdx) {
        Setting_Ring_Silent_Ind(TRUE);
    } else {
        Setting_Ring_Silent_Ind(FALSE);
    }

    g_SettingProfileSettingProfileTimer = NULL;
    g_SettingProfileOperatingIdx = g_SettingProfileActivateIdx;
    Setting_Update_Profile_Para(2, 0);
    Setting_Update_Profile_Para(3, g_SettingProfileOperatingIdx);
    Display_Setting_Timed_Profile_Resume_From_NV();
}

/**
 *
 * Callback of Setting_Profiles_List, KeyCb
 * param (in) obj: lv_obj_t*
 *       (in) key_sta: UI_KEY_STATUS
 *       (in) key_val: UI_KEY_VALUE
 * return UINT8: 0--call system handling outside
 *               1--handled inside
 */
UINT8 Display_Setting_Profiles_List_KeyCb(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val)
{
    UI_Normal_List_Only_Desc_t *NormalLstOnlyDesc = NULL;
    UINT8                      Index              = 0;

    if (KEY_RELEASED != key_sta)
    {
        return(0);
    }

    NormalLstOnlyDesc = (UI_Normal_List_Only_Desc_t *)gp_SettingProfileSettingCurUIDesc;
    Index             = NormalLstOnlyDesc->List.Index;

    if (KEY_DOWN == key_val)
    {
        Index++;
        Index                        %= NormalLstOnlyDesc->List.ListButtonCnt;
        NormalLstOnlyDesc->List.Index = Index;
        return(0);
    }
    else if (KEY_UP == key_val)
    {
        Index += NormalLstOnlyDesc->List.ListButtonCnt;
        Index--;
        Index                        %= NormalLstOnlyDesc->List.ListButtonCnt;
        NormalLstOnlyDesc->List.Index = Index;
        return(0);
    }
    else if (KEY_SOFTRIGHT == key_val)
    {
        Nav_Back(ACT_ID_SETTING_PROFILES_LIST);
        return(1);
    }
    else if (KEY_END == key_val)
    {
        Nav_Home(ACT_ID_ANY);
        return(1);
    }
    else
    {
        return(0);
    }
} /* Display_Setting_Profiles_List_KeyCb */

static VOID Display_Setting_Profiles_List_Restore(VOID *pDesc)
{
    gp_SettingProfileSettingCurUIDesc = (VOID *)pDesc;
}

/**
 * display Setting_Profiles_List
 * param (in) void:
 * return void:
 */
VOID Display_Setting_Profiles_List(VOID)
{
    // Display Setting_Profiles_List
    UI_Normal_List_Only_Desc_t *NormalLstOnlyDesc;
    Nav_Func_List_t            FuncList;

    NormalLstOnlyDesc = (UI_Normal_List_Only_Desc_t *)Hal_Mem_Alloc(sizeof(UI_Normal_List_Only_Desc_t));
    Hal_Mem_Set(NormalLstOnlyDesc, 0, sizeof(UI_Normal_List_Only_Desc_t));

    NormalLstOnlyDesc->TitleBar.TxtId      = PHONE_TEXT_ID_PROFILES;
    NormalLstOnlyDesc->TitleBar.TxtAlign   = LV_LABEL_ALIGN_CENTER;
    NormalLstOnlyDesc->TitleBar.LabelAlign = LV_ALIGN_IN_TOP_LEFT;

    NormalLstOnlyDesc->List.Index         = g_SettingProfileOperatingIdx;
    NormalLstOnlyDesc->List.ListButtonCnt = 5;
    NormalLstOnlyDesc->List.ButtonList    = (UI_Button_Desc_t *)Hal_Mem_Alloc(sizeof(UI_Button_Desc_t) * 5);
    Hal_Mem_Set(NormalLstOnlyDesc->List.ButtonList, 0, sizeof(UI_Button_Desc_t) * 5);

    NormalLstOnlyDesc->List.ButtonList[0].Valid       = TRUE;
    NormalLstOnlyDesc->List.ButtonList[0].ButtonIndex = 0;
    NormalLstOnlyDesc->List.ButtonList[0].TxtId       = PHONE_TEXT_ID_GENERAL;
    NormalLstOnlyDesc->List.ButtonList[0].ReleaseFun  = Display_Setting_Profile_General_Cb;
    NormalLstOnlyDesc->List.ButtonList[1].Valid       = TRUE;
    NormalLstOnlyDesc->List.ButtonList[1].ButtonIndex = 1;
    NormalLstOnlyDesc->List.ButtonList[1].TxtId       = PHONE_TEXT_ID_SILENT;
    NormalLstOnlyDesc->List.ButtonList[1].ReleaseFun  = Display_Setting_Profile_Silent_Cb;
    NormalLstOnlyDesc->List.ButtonList[2].Valid       = TRUE;
    NormalLstOnlyDesc->List.ButtonList[2].ButtonIndex = 2;
    NormalLstOnlyDesc->List.ButtonList[2].TxtId       = PHONE_TEXT_ID_DISCREET;
    NormalLstOnlyDesc->List.ButtonList[2].ReleaseFun  = Display_Setting_Profile_Discreet_Cb;
    NormalLstOnlyDesc->List.ButtonList[3].Valid       = TRUE;
    NormalLstOnlyDesc->List.ButtonList[3].ButtonIndex = 3;
    NormalLstOnlyDesc->List.ButtonList[3].TxtId       = PHONE_TEXT_ID_LOUD;
    NormalLstOnlyDesc->List.ButtonList[3].ReleaseFun  = Display_Setting_Profile_Loud_Cb;
    NormalLstOnlyDesc->List.ButtonList[4].Valid       = TRUE;
    NormalLstOnlyDesc->List.ButtonList[4].ButtonIndex = 4;
    NormalLstOnlyDesc->List.ButtonList[4].TxtId       = PHONE_TEXT_ID_MY_STYLE;
    NormalLstOnlyDesc->List.ButtonList[4].ReleaseFun  = Display_Setting_Profile_MyStyle_Cb;

    NormalLstOnlyDesc->BtnBar.ButtonL.Valid = FALSE;
    NormalLstOnlyDesc->BtnBar.ButtonM.Valid = TRUE;
    NormalLstOnlyDesc->BtnBar.ButtonM.TxtId = PHONE_TEXT_ID_SELECT;
    NormalLstOnlyDesc->BtnBar.ButtonR.Valid = TRUE;
    NormalLstOnlyDesc->BtnBar.ButtonR.TxtId = PHONE_TEXT_ID_BACK;

    NormalLstOnlyDesc->KeyFunc = Display_Setting_Profiles_List_KeyCb;

    // for(i=0;i<NV_SETTING_MAX_PROFILE_NUM;i++)
    // {
    // if(0 != strlen(g_SettingProfileMng->ProfileName[i]))
    // {
    // NormalLstOnlyDesc->List.ButtonList[i].BtnTxt  = Hal_Mem_Alloc(strlen(g_SettingProfileMng->ProfileName[i]));
    // Hal_Mem_Copy(NormalLstOnlyDesc->List.ButtonList[i].BtnTxt), g_SettingProfileMng->ProfileName[i], NV_SETTING_MAX_PROFILE_NAME_LEN);
    // NormalLstOnlyDesc->List.ButtonList[i].TxtId   = LV_LANG_TXT_ID_NONE;
    // }
    // }

    Hal_Mem_Set(&FuncList, 0, sizeof(Nav_Func_List_t));
    FuncList.OnShow    = UI_Normal_List_Only_Create;
    FuncList.OnCreate  = Display_Setting_Profiles_List_Create;
    FuncList.OnDestroy = Display_Setting_Profiles_List_Destroy;
    FuncList.OnRestoreState = Display_Setting_Profiles_List_Restore;

    gp_SettingProfileSettingCurUIDesc = (VOID *)NormalLstOnlyDesc;
    Nav_Forward(ACT_ID_SETTING, ACT_ID_SETTING_PROFILES_LIST, &FuncList, (VOID *)NormalLstOnlyDesc);
} /* Display_Setting_Profiles_List */

VOID Display_Setting_Profiles_List_Cb(lv_obj_t *Btn, lv_event_t Event)
{
    if(LV_EVENT_CLICKED == Event)
    {
        Display_Setting_Profiles_List();
    }
}
